/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.editor;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Level;
import org.pircbotx.Colors;

import com.google.code.cubeirc.common.ApplicationInfo;
import com.google.code.cubeirc.debug.DebuggerQueue;

import lombok.Getter;

public class TemplateManager {

	@Getter
	public static final ConcurrentHashMap<String, String> templatestring = new ConcurrentHashMap<String, String>();
	
	
	/**
	 * Add to Template manager variable to replace EX: TEST = "HELLO"
	 * @param key
	 * @param value
	 */
	public static void addTemplate(String key, String value)
	{
		getTemplatestring().put(key, value);
	}
	
	public static void init()
	{
		DebuggerQueue.addDebug(TemplateManager.class.getName(), Level.INFO,"Setup Template manager default variables");
		Customs cs = new Customs();
		getTemplatestring().put("APP_NAME", ApplicationInfo.APP_NAME);
		getTemplatestring().put("APP_FULL", ApplicationInfo.APP_FULL);
		getTemplatestring().put("APP_WEB", ApplicationInfo.APP_WEB);
		getTemplatestring().put("APP_VERSION", ApplicationInfo.APP_VERSION);		
		getTemplatestring().put("AUTHOR_NAME", ApplicationInfo.APP_AUTHORS[0]);	
		getTemplatestring().put("AUTHOR_WEB", ApplicationInfo.APP_WEB);
		getTemplatestring().put("APP_TICKETS_URL", ApplicationInfo.APP_TICKETS_URL);

		
		
		//Setup color replacing ex: color_red => \u000304
		for (Entry<String, String> e : cs.ircColorsStr.entrySet()) {
			getTemplatestring().put("color_" + e.getKey(), e.getValue());
		}
		getTemplatestring().put("bold", Colors.BOLD);
		getTemplatestring().put("underline", Colors.UNDERLINE);
		getTemplatestring().put("normal", Colors.NORMAL);
		getTemplatestring().put("italic", "\u0016");
		
		
		DebuggerQueue.addDebug(TemplateManager.class.getName(), Level.INFO,"Setup Template manager completed: %s entries", getTemplatestring().size());
				
		
	}
	
	/**
	 * Find in string variables and replace
	 * @param value
	 * @return
	 */
	public static String replace(String value)
	{
		String s = value;
		for (Map.Entry<String, String> e : getTemplatestring().entrySet()) {
		  s = s.replaceAll("\\[" + e.getKey() + "\\]", e.getValue());
		}
		  return s;
	}
}
